package com.famdoc.Controller;
import com.famdoc.Exception.BlacklistService;
import com.famdoc.Utils.JwtTokenUtil;
import com.famdoc.Pojo.dto.Result;
import com.famdoc.Pojo.entity.Doctor;
import com.famdoc.Service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/doctor")
public class DoctorController {
  @Autowired
  private DoctorService doctorService;
  @Autowired
  private JwtTokenUtil jwtTokenUtil;
  @Autowired
  private BlacklistService blacklistService;
  @GetMapping ("/selectDoctor")
  public Result selectDoctor( String account, String password){
         Doctor doctor=doctorService.selectDoctor(account,password);
      if(doctor!=null){
          String token = jwtTokenUtil.generateToken(doctor.getName(), 30 * 60 * 1000);
          Map<String, Object> loginInfo= new HashMap<>();
          loginInfo.put("token",token);
          loginInfo.put("doctorInfo",doctor);
          return Result.success(loginInfo);
      }
          else{
            return   Result.error(101,"用户不存在");
      }
  }
  @PostMapping("/updatePassword")
  public Result updatePassword(int id,String newPassword,@RequestHeader("Authorization") String token){

        doctorService.updatePassword(id,newPassword);
        blacklistService.addToBlacklist(token);
        return Result.success();
  }

  @PutMapping("/updateDoctorInfo")
  public Result updateDoctorInfo(@RequestBody Doctor doctor){
      return doctorService.updateDoctorInfo(doctor);
  }


  @PostMapping("/logOut")
  public Result logOut(@RequestHeader("Authorization") String token){
      blacklistService.addToBlacklist(token);
      System.out.println(token);
      return Result.success();
  }

  //拦截器
  @RestControllerAdvice
  public class TokenInterceptor {

      @Autowired
      private JwtTokenUtil jwtTokenUtil;

      @Autowired
      private HttpServletRequest request;

      @Autowired
      private BlacklistService blacklistService;

      @ExceptionHandler(Exception.class)
      public Result handleException(Exception e) {
          if(e.getMessage().contains("Token已失效，请重新登录") ||e.getMessage().contains("Token无效或已过期")){
              return Result.error(401,e.getLocalizedMessage());
          }else{
              return Result.error(101,e.getLocalizedMessage());
          }
      }

      @ModelAttribute
      public void interceptRequest() throws Exception {
          String token = request.getHeader("Authorization");

          // 排除登录接口的请求
          if (!request.getRequestURI().equals("/doctor/selectDoctor")) {
              if (token != null && jwtTokenUtil.validateToken(token)) {
                  if (blacklistService.isBlacklisted(token)) {
                      throw new Exception("Token已失效，请重新登录");
                  }
              } else {
                  throw new Exception("Token无效或已过期");
              }
          }
      }
  }












}
